package com.sunnada.edu.system.sysdict.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.common.MD5;
import com.sunnada.edu.system.pub.dao.Page;
import com.sunnada.edu.system.pub.service.PubMethods;
import com.sunnada.edu.system.pub.util.Utils;
import com.sunnada.edu.system.sysdict.bean.SysDict;
import com.sunnada.edu.system.sysdict.service.SysDictMethods;
import com.sunnada.edu.system.sysdict.service.SysDictService;

/***
 * explain 控制层 
 * @author chs 2015-5-12
 * version 1.0
 */
@Controller
@RequestMapping("/sysdictController")
public class SysDictController extends BasicController{
	@Resource
	SysDictService dictService;


	/**数据字典首页*/
	@RequestMapping("/dict")
	public String dict(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		List typeList=dictService.getDictTypeList(params);
		request.setAttribute("typeList", typeList);
		//当前路径
		String moduleId=request.getParameter("moduleId");
		request.setAttribute("pathValue", PubMethods.getPathValue(moduleId));
		return "system/sysdict/sysdict";
	}
	/**数据字典查询*/
	@RequestMapping("/dictSearch")
	public void dictSearch(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, SysDict.class, params);
		Page page = new Page(); 
		page.setRowCount(dictService.getDictCots(params));
		page.setPageMap(request, page, params);
		List<?> dictList = dictService.findDicts(params);
		JSONObject json = new JSONObject();		
		json.put("list", dictList);
		json.put("params", params); 
		printInfo(response,json);
	}
		
	@RequestMapping("/typeAddPage")
	public String typeAddPage(HttpServletRequest request) { 
		return "system/sysdict/sysdict_type_add";
	}
	
	@RequestMapping("dictAddPage")
	public String dictAddPage(HttpServletRequest request) { 
		HashMap<String, String> params = new HashMap<String, String>();
		List typeList=dictService.getDictTypeList(params);
		request.setAttribute("typeList", typeList);
		return "system/sysdict/sysdict_add";
	}
	
	/**数据字典修改页面*/
	@RequestMapping("/dictUpdate")
	public String dictUpdate(HttpServletRequest request) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, SysDict.class, params);
		Map dict = dictService.getDictByRecId(params);
	    request.setAttribute("dict", dict);
	    List typeList=dictService.getDictTypeList(params);
		request.setAttribute("typeList", typeList);
		return "system/sysdict/sysdict_update";
	}


	@RequestMapping("/dictSave")
	public void dictSave(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, SysDict.class, params);
		//验证数据字典账号是否重复
		String recId=params.get("recId");
		if(Utils.isNotNull(recId)){
			msg = dictService.dictUpdate(params);
		}else{
			msg = dictService.dictAdd(params);
		}
		printInfo(response,msg);
	}
	
	@RequestMapping("/typeSave")
	public void typeAdd(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, SysDict.class, params);
		//验证数据字典账号是否重复
		String recId=params.get("recId");
		if(Utils.isNotNull(recId)){
			msg = dictService.dictUpdate(params);
		}else{
			msg = dictService.typeAdd(params);
		}
		printInfo(response,msg);
	}
	/**数据字典删除功能*/
	@RequestMapping("/dictDelete")
	public void dictDelete(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds",Utils.getSqlSplitStr(recIds));
		msg = dictService.dictDelete(params);		
		printInfo(response,msg);
	}

	
	/**数据字典重置密码功能*/
	@RequestMapping("/resetPwd")
	public void resetPwd(HttpServletRequest request,HttpServletResponse response) { 
		JSONObject returnJson = new JSONObject();
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		String pwd = "888888";//默认密码
		params.put("recIds",recIds);
		params.put("pwd",pwd);
		//msg = dictService.resetPwd(params);	 
		printInfo(response,msg);
	}
	
	/**数据字典授权页面*/
	@RequestMapping("/grentPage")
	public String grentPage(HttpServletRequest request) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, SysDict.class, params);
		//查找当前数据字典有哪些角色
		//List<HashMap<String, String>> roleList=dictService.getMyRoleList(params);
		//request.setAttribute("roleList", roleList);
		request.setAttribute("dictId", params.get("dictId"));
		return "system/sysdict/sysdict_set_role";
	}
	
	
	/**所选数据字典有哪些权限*/
	@RequestMapping("/getSelectDictRoles")
	public void getSelectDictRoles(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, SysDict.class, params);
		String dictId = request.getParameter("dictId");
		params.put("dictId", dictId);
		//List  dictRoles = dictService.getSelectDictRole(params);		
		JSONObject json = new JSONObject();		
		//json.put("dictRoles", dictRoles);
		json.put("params", params); 
		printInfo(response,json);
	}
	
	/**数据字典授权*/
	@RequestMapping("/roleAdd")
	public void roleAdd(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, SysDict.class, params);
		String roleIds = request.getParameter("roleIds");
		String dictId = request.getParameter("dictId");
		params.put("dictId", dictId);
		params.put("roleIds", roleIds);	
		printInfo(response,msg);
	}
	
	/**数字字典大类首页*/
	@RequestMapping("/sysdictType")
	public String dictBroad(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		List typeList=dictService.getDictTypeList(params);
		request.setAttribute("typeList", typeList);
		return "system/sysdict/sysdict_type";
	}
	/**数字字典大类查询*/
	@RequestMapping("/dictTypeSearch")
	public void dictTypeSearch(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, SysDict.class, params);
		Page page = new Page(); 
		page.setRowCount(dictService.getDictTypeCots(params));
		page.setPageMap(request, page, params);
		List<?> dictList = dictService.findDictTypes(params);
		JSONObject json = new JSONObject();		
		json.put("list", dictList);
		json.put("params", params); 
		printInfo(response,json);
	}
	/**字典大类修改页面*/
	@RequestMapping("/dictTypeUpdate")
	public String dictTypeUpdate(HttpServletRequest request) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, SysDict.class, params);
		Map dict = dictService.getDictTypeByRecId(params);
	    request.setAttribute("dict", dict);
		return "system/sysdict/sysdict_type_update";
	}
	/**字典大类修改数据*/
	@RequestMapping("/typeUpdateSave")
	public void typeUpdateSave(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, SysDict.class, params);
		msg = dictService.typeUpdateSave(params);
		printInfo(response,msg);
	}
	
	/**数据字典删除功能*/
	@RequestMapping("/dictTypeDelete")
	public void dictTypeDelete(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds",Utils.getSqlSplitStr(recIds));
		msg = dictService.dictTypeDelete(params);		
		printInfo(response,msg);
	}
	
	/**适用于下拉列表*/
	@RequestMapping("/getDictListByType")
	public void getDictListByType(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, SysDict.class, params);
		List<?> dictList = SysDictMethods.getDictListByType(params);
		JSONObject json = new JSONObject();		
		json.put("list", dictList);
		json.put("params", params); 
		printInfo(response,json);
	}
}
